package com.vehicle.stock.provider.rocketmq.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionCheckListener;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by yantingbin on 2017/8/11.
 */
@Component
public class TransactionProducer {
    private static final TransactionMQProducer PRODUCER = new TransactionMQProducer("please_rename_unique_group_name");
    private static final TransactionCheckListener transactionCheckListener = new TransactionCheckListenerImpl();

    public static TransactionMQProducer getMqProducer(){
        return PRODUCER;
    }

    @PostConstruct
    public void init() throws MQClientException {
        PRODUCER.setNamesrvAddr("172.16.14.76:9876");
        PRODUCER.setCheckThreadPoolMinSize(2);
        PRODUCER.setCheckThreadPoolMaxSize(2);
        PRODUCER.setCheckRequestHoldMax(2000);
        PRODUCER.setTransactionCheckListener(transactionCheckListener);
        PRODUCER.start();
    }

    @PreDestroy
    public void destory() {
        PRODUCER.shutdown();
    }
}
